package Handlers;

import java.io.PrintWriter;
import java.util.StringTokenizer;

import javax.swing.JTextArea;

import ServerSide.ClientHandling;
import ServerSide.ClientSock;

/**
 * @author Avishai Kopeliansky 
 * @ID 066717752
 * @author David Shimshon
 * @ID 038185617
 */
public class ServerHandler extends AbsHandler{
	protected ClientHandling ch ;
	protected ClientSock csock;
	protected PrintWriter pw;
	protected final int _DC = 4;


	/**
	 * Constructor 
	 * @param ch ClientHandling representative
	 * @param csock ClientSocket representative
	 * @param pw PrintStream Writer representative
	 */
	public ServerHandler(ClientHandling ch,ClientSock csock,PrintWriter pw)
	{
		this.ch = ch;
		this.csock = csock;
		System.out.println(csock == null);
		this.pw = pw;

	}

	@Override
	//Personal Message
	protected void PMSender(String body, JTextArea jta) {
		StringTokenizer st = new StringTokenizer(body, "<>");
		String user = st.nextToken();
		if(!this.ch.sendMsg(user,st.nextToken()))
		{
			this.pw.println("<msg><there is no client: " + user + " >\n");
		}

	}

	@Override
	//System Message
	protected void SMSender(String body, JTextArea jta) {
		// TODO Auto-generated method stub
		String t = "@Server:";
		StringTokenizer st = new StringTokenizer(body,"<>");
		t = t + st.nextToken();
		this.ch.broadCast(t);
	}

	@Override
	//Universal or Broadcast message
	protected void UMSender(String body, JTextArea jta) {
		this.ch.broadCast(body);

	}
	//disconnect request
	protected void DCSender(String body, JTextArea jta) {
		this.pw.println("<msg><"+csock.getNames()+" Bye Bye>");
		this.ch.broadCast(csock.getNames()+" Has Left the server");
		csock.shutDown();
		ch.checkAlive();

	}
	//User List Request
	protected void LSender(String body, JTextArea jta) {
		this.pw.println("<UserList><" + this.ch.clientList());

	}

	@Override
	//Deprecated method, should'nt be used and wont be used, left due to Inheritance
	protected void COSender(String body, PrintWriter pw, JTextArea jta) {
	
		System.out.println("How or why did you get here?");


	}

	@Override
	//Set the state of the sorter
	protected void Stater(String header) {
		if(header.equalsIgnoreCase("msg"))
		{
			_STAT = _PMSG;
		}
		else if(header.equalsIgnoreCase("umsg")) 
		{
			_STAT = _UMSG;
		}
		else if(header.equalsIgnoreCase("smsg")) 
		{
			_STAT = _SMSG;
		}
		
		else if(header.equalsIgnoreCase("Disconnect"))
		{
			_STAT = _DC;
		}
		else 
		{
			_STAT = _ULIST;
		}
	}

	/**
	 * Sorter Method, will analize the header and deal with the body
	 * @param header Header of the String, will let us know the type of message we're dealing with
	 * @param body Body of the message
	 * @param pw PrintWriter 
	 * @param jta ----Deprecated----
	 */
	public void sorter(String header, String body, PrintWriter pw,JTextArea jta) {
		Stater(header);
		switch(_STAT){		
		case 0:
			UMSender(body, jta);
			break;
		case 1:
			PMSender(body, jta);
			break;
		case 2:
			SMSender(body, jta);
			break;
		case 3:
			LSender(body, jta);
			break;
		case 4:
			DCSender(body, jta);
			break;
			
			
		}

	}



}
